package com.tiza.xiaoyoubao.ui.fragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.TaskInfo;
import com.tiza.xiaoyoubao.entity.TaskOrderInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/4/10.
 * 垫付赚金
 */

public class DianFuMakeMoneyFragment extends BaseFragment {

    @BindView(R.id.lv_listView)
    ListView mListView;
    private List<TaskInfo> mList;
    private MyAdapter mAdapter;
    private List<TaskOrderInfo> infoList;
    private List<TaskOrderInfo> infoListDn;
    private List<TaskOrderInfo> infoListTm;
    private List<TaskOrderInfo> infoListTeB;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dianfu_makemoney;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        getTaskInfo();
        getTaskInfoDn();
        getTaskInfoTm();
        getTaskInfoTeB();
        mList = new ArrayList<>();
        mList.add(new TaskInfo(R.mipmap.taobao, "手 机 淘 宝 任 务", "手机淘宝任务"));
        mList.add(new TaskInfo(R.mipmap.taobao, "电 脑 淘 宝 任 务", "电脑淘宝任务"));
        mList.add(new TaskInfo(R.mipmap.tianmao, "手 机 天 猫 任 务", "手机天猫任务"));
        mList.add(new TaskInfo(R.mipmap.tebie, "特 别 任 务", "特别任务"));
        mList.add(new TaskInfo(R.mipmap.jingdong, "手 机 京 东 任 务", "手机京东任务"));
        mList.add(new TaskInfo(R.mipmap.mogujie, "手 机 蘑 菇 街 任 务", "手机蘑菇街任务"));
        mList.add(new TaskInfo(R.mipmap.meilishuo, "手 机 美 丽 说 任 务", "手机美丽说任务"));
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (position == 0) {
                    UIHelper.showMpTaoBaoTask(mActivity);
                    finish();
                } else if (position == 1) {
                    UIHelper.showDnTaoBaoTask(mActivity);
                    finish();
                } else if (position == 2) {
                    UIHelper.showMpTMTask(mActivity);
                    finish();
                } else if (position == 3) {
                    UIHelper.showDianFuTbTask(mActivity);
                    finish();
                } else if (position == 4) {
                    toast("功能部署中，敬请期待");
//                    UIHelper.showMpJDTask(mActivity);
                } else if (position == 5) {
                    toast("功能部署中，敬请期待");
//                    UIHelper.showMpMGJTask(mActivity);
                } else if (position == 6) {
                    toast("功能部署中，敬请期待");
//                    UIHelper.showMpMLSTask(mActivity);
                }
            }
        });
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(getContext(), R.layout.item_dianfu_makemoney, null);
                holder = new ViewHolder();
                holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_newTask);
                holder.tvContent = (TextView) convertView.findViewById(R.id.tv_newTask_Content);
                holder.mImageView = (ImageView) convertView.findViewById(R.id.imageView);
                holder.tvNum= (TextView) convertView.findViewById(R.id.tv_num);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvTitle.setText(mList.get(position).getTitle());
            holder.tvContent.setText(mList.get(position).getConnect());
            holder.mImageView.setImageResource(mList.get(position).getImage());
            if(position==0){
                if(infoList==null){
                    holder.tvNum.setVisibility(View.GONE);
                }else{
                    holder.tvNum.setVisibility(View.VISIBLE);
                    holder.tvNum.setText("("+infoList.size()+")");
                }
            }else if(position==1){
                if(infoListDn==null){
                    holder.tvNum.setVisibility(View.GONE);
                }else{
                    holder.tvNum.setVisibility(View.VISIBLE);
                    holder.tvNum.setText("("+infoListDn.size()+")");
                }
            }else if(position==2) {
                if (infoListTm == null) {
                    holder.tvNum.setVisibility(View.GONE);
                } else {
                    holder.tvNum.setVisibility(View.VISIBLE);
                    holder.tvNum.setText("(" + infoListTm.size() + ")");
                }
            }else if(position==3) {
                if (infoListTeB == null) {
                    holder.tvNum.setVisibility(View.GONE);
                } else {
                    holder.tvNum.setVisibility(View.VISIBLE);
                    holder.tvNum.setText("(" + infoListTeB.size() + ")");
                }
            }
            return convertView;
        }
    }

    static class ViewHolder {
        public TextView tvTitle;
        public TextView tvContent;
        public ImageView mImageView;
        public TextView tvNum;

    }

    //获取任务个数
    private void getTaskInfo() {
        ApiHttpClient.getTaskInfo(this, "1",
                new JsonCallback<TaskOrderInfo>(TaskOrderInfo.class) {

                    @Override
                    public void onBefore(Request request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        toast("任务信息获取失败");
                    }

                    @Override
                    public void onResponse(ResponseInfo<TaskOrderInfo> response) {
                        int code = response.getCode();
                        String message = response.getMessage();
                        if (code == 1) {
                            showWarnDialog(message);
                            return;
                        }
                        infoList = response.getData();
                        mAdapter = new MyAdapter();
                        mListView.setAdapter(mAdapter);
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                    }
                }
        );
    }
    //获取任务个数
    private void getTaskInfoDn() {
        ApiHttpClient.getTaskInfo(this, "4",
                new JsonCallback<TaskOrderInfo>(TaskOrderInfo.class) {

                    @Override
                    public void onBefore(Request request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        toast("任务信息获取失败");
                    }

                    @Override
                    public void onResponse(ResponseInfo<TaskOrderInfo> response) {
                        int code = response.getCode();
                        String message = response.getMessage();
                        if (code == 1) {
                            showWarnDialog(message);
                            return;
                        }
                        infoListDn = response.getData();
                        mAdapter = new MyAdapter();
                        mListView.setAdapter(mAdapter);
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                    }
                }
        );
    }
    //获取任务个数
    private void getTaskInfoTm() {
        ApiHttpClient.getTaskInfo(this, "2",
                new JsonCallback<TaskOrderInfo>(TaskOrderInfo.class) {

                    @Override
                    public void onBefore(Request request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        toast("任务信息获取失败");
                    }

                    @Override
                    public void onResponse(ResponseInfo<TaskOrderInfo> response) {
                        int code = response.getCode();
                        String message = response.getMessage();
                        if (code == 1) {
                            showWarnDialog(message);
                            return;
                        }
                        infoListTm = response.getData();
                        mAdapter = new MyAdapter();
                        mListView.setAdapter(mAdapter);
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                    }
                }
        );
    }
    //获取任务个数
    private void getTaskInfoTeB() {
        ApiHttpClient.getSellerTaskEcpinfoAll(this,
                new JsonCallback<TaskOrderInfo>(TaskOrderInfo.class) {

                    @Override
                    public void onBefore(Request request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        toast("任务信息获取失败");
                    }

                    @Override
                    public void onResponse(ResponseInfo<TaskOrderInfo> response) {
                        int code = response.getCode();
                        String message = response.getMessage();
                        if (code == 1) {
                            showWarnDialog(message);
                            return;
                        }
                        infoListTeB = response.getData();
                        mAdapter = new MyAdapter();
                        mListView.setAdapter(mAdapter);
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                    }
                }
        );
    }
}
